<?php

namespace App\Imports;

use App\Models\FileGoods;
use App\Services\GoodsService;
use App\Utils\Code;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;

class GoodsImport implements ToModel, WithBatchInserts, WithChunkReading, ToArray
{
    private $file;
    private $index = 0;

    /**
     * OrderComparedImport constructor.
     * @param FileGoods|null $file
     */
    public function __construct(FileGoods $file = null) {
        $this->file = $file;
    }

    public function array(array $array)
    {
        // TODO: Implement array() method.
        unset($array[0]);
    }

    public function model(array $row)
    {
        // TODO: Implement model() method.
        $this->index++;
        $fields = [
            'goods_code',
            'goods_name',
            'images',
            'stock',
            'weight',
            'seat',
            'label_price',
            'supplier_id',
            'remarks',
            'is_onshelf',
            'is_bid',
            'is_show'
        ];
        try {
            if ($row[0] == '标号') {
                return null;
            }
            $data = [];
            foreach ($fields as $key => $field) {
                if (isset($row[$key]) && $row[$key]) {
                    if ($field === 'is_onshelf') {
                        $data[$field] = $row[$key] === '下架' ? 0 : 1;
                    } elseif ($field === 'is_bid' || $field === 'is_show') {
                        $data[$field] = $row[$key] === '否' ? 0 : 1;
                    }  elseif ($field === 'images') {
                        $data[$field] = [$row[$key]];
                    } elseif ($key ==='goods_name' || $key ==='description' || $key ==='remarks') {
                        $data[$field] = (string)$row[$key];
                    } else {
                        $data[$field] = $row[$key];
                    }
                }
            }
            if (!$data) {
                throw new \Exception('没有获取到有效的导入数据');
            } else {
                try {
                    $data['user_id'] = $this->file->user_id;
                    (new GoodsService())->store($data);
                    $this->file->increment('success_total');
                } catch (\Exception $exception) {
                    throw new \Exception($exception->getMessage());
                }
            }
        } catch (\Throwable $throwable) {
            \DB::table('file_goods_error')->insert([
                'file_id' => $this->file->file_id,
                'index' => $this->index,
                'error' => $throwable->getMessage()
            ]);
            $this->file->increment('failure_total');
        }
    }

    public function batchSize(): int
    {
        // TODO: Implement batchSize() method.
        return 1;
    }

    public function chunkSize(): int
    {
        // TODO: Implement chunkSize() method.
        return 1;
    }
}
